package com.zdp.netty.chapter13;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.FileRegion;
import io.netty.channel.SimpleChannelInboundHandler;

import java.io.File;
import java.io.RandomAccessFile;

/**
 * @Author zhengdp
 * @Description
 */
public class FileServerHandler extends SimpleChannelInboundHandler<String> {

    private static final String CR = System.getProperty("line.separator");

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
        File file = new File(msg);
        if(file.exists()) {
            if(!file.isFile()) {
                ctx.writeAndFlush("Not a file : " + file + CR);
                return ;
            }
            ctx.write(file + " " + file.length() + CR);
            RandomAccessFile randomAccessFile = new RandomAccessFile(msg,"r");
            // 入参：1. 文件通道  2. 文件操作的指针位置，读取或者写入的起始点  3.操作的总字节数
            FileRegion region = new DefaultFileRegion(
                    randomAccessFile.getChannel(),0,randomAccessFile.length()
            );
            ctx.write(region);
            ctx.writeAndFlush(CR);
            randomAccessFile.close();
        } else {
            ctx.writeAndFlush("File not found: " + file + CR);
        }
    }
}
